package cn.stylefeng.guns.utils.map;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class LocalUtils {
    private static void printNode(Node n, int id,int parentId) {
        if(parentId == -1){
            System.out.println("insert into flow_country(country_id,country_name, count_abbr)values( "
                            + "\""+ id +"\","
                            + "\""+ n.getAttributes().getNamedItem("Name").getNodeValue()+"\","
                            + "\""+ n.getAttributes().getNamedItem("Code").getNodeValue()+"\""
                            +");"
            );
        }else{
            System.out.println("insert into flow_country_city(country_id,city_name, city_addr)values( "
                    + "\""+ parentId +"\","
                    + "\""+ n.getAttributes().getNamedItem("Name").getNodeValue()+"\","
                    + "\""+ n.getAttributes().getNamedItem("Code").getNodeValue()+"\""
                    +");"
            );
        }
    }




    //https://blog.csdn.net/heganlin/article/details/52516611

    public static void main(String[] args) throws Exception {
        LocalUtils sysWorldCountry = new LocalUtils();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse("file:///H:/云客科技/物流项目/LocList.xml");
        NodeList l = document.getElementsByTagName("CountryRegion");
        System.out.println("一共有" + l.getLength() + "个国家");
        for (int i = 0; i < l.getLength(); i++) {
            Node n = l.item(i);
            int pid = i + 1;
            printNode(n,pid,-1);
            NodeList childNodes = n.getChildNodes();
            for (int k = 0; k < childNodes.getLength(); k++) {
                Node n2 = childNodes.item(k);
                if("State".equals(n2.getNodeName())){
                    if(n2.getAttributes().getNamedItem("Name")==null){
                        NodeList childNodesS = n2.getChildNodes();
                        for(int z = 0; z < childNodesS.getLength(); z++) {
                            Node ns = childNodesS.item(z);
                            if("City".equals(ns.getNodeName())){
//                              System.out.println("这个国家没有州");
                                printNode(ns,-1,pid);
                            }
                        }
                        continue;
                    }
                    printNode(n2,-1,pid);
//                    NodeList childNodes2 = n2.getChildNodes();
//                    for(int u = 0; u < childNodes2.getLength(); u++) {
//                        Node n3 = childNodes2.item(u);
//                        if("City".equals(n3.getNodeName())){
//                            printNode(n3,n2,3);
//                        }
//                    }
                }
            }
        }
    }
}
